﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using CommClass;
using DMS.CommClass;


namespace DMS
{
    public partial class Frmxitongbuhuo : DMS.FrmTemplate 
    {

        CaigouDAO cg = new CaigouDAO(App.Ds);
        MO mo = new MO(App.Ds);
        int pageSize = App.pagesize;
        int currentPage = 0;
        int totalpage = 0;  
        string sqlstr;
        string orderBy = " h.StoreID desc ,c.SupplySupplierID,a.SupplierID,a.ProdID "; 
        bool flag;
        public Frmxitongbuhuo( )   
        {
            InitializeComponent();
            this.GetProductType();

            
        }


        private void GetProductType()
        {
            //商品分类下拉
            DataTable dt = mo.GetProductType();
            DataRow dr = dt.NewRow();
            dr["ProdTypeID"] = "0";
            dr["ProdTypeName"] = "请选择";
            dt.Rows.InsertAt(dr, 0);
            //下拉数据绑定
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "ProdTypeName";
            comboBox1.ValueMember = "ProdTypeID";
            //默认选中
            comboBox1.SelectedIndex = 0;

        }
        
   

       

        string checkvehiclenum;
        string carriername;
        string vehcount;
        bool result;
        private bool CheckVehilce()
        {
                      
            for (int i = 0; i < dgvdo.RowCount; i++)
            {
                //判断是否选中
                if (dgvdo.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True")
                {
                    checkvehiclenum = dgvdo.Rows[i].Cells[3].Value.ToString();
                    carriername = dgvdo.Rows[i].Cells[5].Value.ToString();
                    vehcount = dgvdo.Rows[i].Cells[4].Value.ToString();
                }
            }
            for (int i = 0; i < dgvdo.RowCount; i++)
            {
                //判断是否选中
                if (dgvdo.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True")
                {
                    if (checkvehiclenum != dgvdo.Rows[i].Cells[3].Value.ToString() || carriername != dgvdo.Rows[i].Cells[5].Value.ToString() || vehcount !=dgvdo.Rows[i].Cells[4].Value.ToString())
                    {
                        result = false;
                        break;
                    }
                    else
                    {
                        result = true;
                    }

                }
            }
            

            return result;
        }

       



        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       

        

        private void button2_Click(object sender, EventArgs e)
        {
           
            this.getcheck();
            DataTable dt = cg.getbuhuoproduct( sqlstr, orderBy);
            this.dgvdo.DataSource = dt;
            if (this.dgvdo.ColumnCount > 1)
            {
                this.dgvdo.Columns[1].Visible = false;
                this.dgvdo.Columns[2].ReadOnly = true;
                this.dgvdo.Columns[3].ReadOnly = true;
                this.dgvdo.Columns[4].ReadOnly = true;
                this.dgvdo.Columns[5].ReadOnly = true;
                this.dgvdo.Columns[6].ReadOnly = true;
                this.dgvdo.Columns[7].ReadOnly = true;
                this.dgvdo.Columns[8].ReadOnly = true;
                this.dgvdo.Columns[9].ReadOnly = true; 
                this.dgvdo.Columns[10].Visible = false;
                this.dgvdo.Columns[11].Visible = false;
                this.dgvdo.Columns[12].Visible = false;
                this.dgvdo.Columns[13].Visible = false;
                this.dgvdo.Columns[14].Visible = false;
                this.dgvdo.Columns[15].ReadOnly = true;
                this.dgvdo.Columns[16].ReadOnly = true;
                this.dgvdo.Columns[17].Visible = false;
                this.dgvdo.Columns[18].Visible = false;
                this.dgvdo.Columns[19].Visible = false;
                this.dgvdo.Columns[20].ReadOnly = true;

            }
           
        }


        private void getcheck()
        {
            StringBuilder strSql111 = new StringBuilder();

         
            
            if(int.Parse(comboBox1.SelectedValue.ToString())>0)
            {
                strSql111.Append("  a.ProdTypeID =  " + comboBox1.SelectedValue.ToString());
            }
            
            if (textBox4.Text != null && textBox4.Text.Trim()!="")
            {
                if (strSql111 != null && strSql111.ToString() != "")
                {
                    strSql111.Append(" and ");
                }
                strSql111.Append(" a.ProdCode  like  '%" + textBox4.Text.Trim() + "%'");
            }
            
            if (textBox3.Text != null && textBox3.Text.Trim() != "")
            {
                if (strSql111 != null && strSql111.ToString() != "")
                {
                    strSql111.Append(" and ");
                }
                strSql111.Append(" a.ProdName  like  '%" + textBox3.Text.Trim() + "%'");
            }


            sqlstr = strSql111.ToString();
        }




        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
        {
            //if (e.ColumnIndex == -1 || e.RowIndex == -1) return;
           // int x = this.dgvdo.CurrentCell.ColumnIndex;//获取鼠标的点击列
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.dgvdo.RowCount > 0)
            {
                //生成采购单
                using (SqlConnection conn = new SqlConnection(App.GetSqlConnection()))
                {
                    conn.Open();
                    using (SqlTransaction tran = conn.BeginTransaction()) //开始数据库事务。即创建一个事务对象tran  
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.Connection = conn;
                            cmd.Transaction = tran; //获取或设置将要其执行的事务  
                           // try
                         //   {
                                Caigou cgd = new Caigou();
                                int iii = 0;
                                int CaigouIDiiii   = 0;   
                                for (int i = 0; i < this.dgvdo.Rows.Count; i++)
                                {
                                    if (this.dgvdo.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True")
                                    {
                                        //生成采购单
                                      

                                        //生成采购单


                                        if (i == 0 || (i > 0 && CaigouIDiiii == 0) || (i > 0 && iii != 0 && (this.dgvdo.Rows[i].Cells["供应商id"].Value.ToString() != this.dgvdo.Rows[iii].Cells["供应商id"].Value.ToString() || this.dgvdo.Rows[i].Cells["货主"].Value.ToString() != this.dgvdo.Rows[iii].Cells["货主"].Value.ToString() || this.dgvdo.Rows[i].Cells["仓库id"].Value.ToString() != this.dgvdo.Rows[iii].Cells["仓库id"].Value.ToString())))
                                        {

                                            cgd.CaigouNumber = cgd.MakeCaigouNumber(conn, cmd);
                                            cgd.ZhiDanDate = DateTime.Now;
                                            cgd.CaigouDate = DateTime.Now;
                                            cgd.UserID = App.AppUser.UserID;
                                            cgd.CaigouTypeID = App.caigouputong;
                                            cgd.CaigouStatusID = App.newstatus;
                                            cgd.SupplierID = int.Parse(this.dgvdo.Rows[i].Cells["货主id"].Value.ToString());
                                            cgd.SupplySupplierID = int.Parse(this.dgvdo.Rows[i].Cells["供应商id"].Value.ToString());
                                            if (App.AppUser.CompanyID == null || App.AppUser.CompanyID == 0)
                                            {
                                                cgd.CarrierID = 1;
                                            }
                                            else
                                            {
                                                cgd.CarrierID = App.AppUser.CompanyID;
                                            }
                                       

                                            cgd.CaigourenID = App.AppUser.UserID;
                                            cgd.StoreID = int.Parse(this.dgvdo.Rows[i].Cells["仓库id"].Value.ToString());
                                            cgd.TuiHuoKeHu = "";
                                            cgd.TuiHuoRemark = "";
                                            cgd.Remark = "";
                                            cgd.CaigouID = cgd.saveCaigou(cgd, conn, cmd);
                                            CaigouIDiiii = cgd.CaigouID;
                                        }

                                        iii = i;

                                            //插入明细
                                            CaigouEntry cae = new CaigouEntry();
                                            cae.CaigouID = cgd.CaigouID;
                                            cae.ProdID = int.Parse(this.dgvdo.Rows[i].Cells["商品id"].Value.ToString());

                                            double pingjun = double.Parse(this.dgvdo.Rows[i].Cells["平均"].Value.ToString());

                                            double anquantianshu = double.Parse(this.dgvdo.Rows[i].Cells["安全天数"].Value.ToString());

                                            double zuidatianshu = double.Parse(this.dgvdo.Rows[i].Cells["最大天数"].Value.ToString());

                                            double StockUseableQty = double.Parse(this.dgvdo.Rows[i].Cells["可用库存"].Value.ToString());

                                            double CaigouShuLiang = double.Parse(this.dgvdo.Rows[i].Cells["采购数量"].Value.ToString());

                                            cae.CaigouShuLiang = pingjun * zuidatianshu - StockUseableQty - CaigouShuLiang;
                                            cae.Caigoujia = double.Parse(this.dgvdo.Rows[i].Cells["采购价"].Value.ToString());

                                            cae.Remark = "";
                                            cae.saveCaigouEntry(cae, conn, cmd); 


                                    }
                                }
                                 

                                tran.Commit();
                                conn.Close();

                                MessageBox.Show("生成成功");
                           // }
                        //    catch
                         //   {

                       //         tran.Rollback();//如果执行不成功，发送异常，则执行rollback方法，回滚到事务操作开始之前。
                       //         conn.Close();
                        //        MessageBox.Show("生成失败");
                        //    }

                        }
                    }
                }

                this.Close();
            }
        }

        private void FrmAddProduct_Load(object sender, EventArgs e)
        {

        }     
     
       
      
    }
}
